//
//  ReversiInitializeController.m
//  Reversi
//
//  Created by 成瀬 龍人 on 2/9/10.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "ReversiInitializeController.h"
#import "ReversiVirtualPlayers.h"
#import "ReversiPlayer.h"
#import "ReversiBoardViewString.h"

@implementation ReversiInitializeController
- (id)init{
	self->blackSelectedItemIndex = 0;
	self->whiteSelectedItemIndex = 0;
	return self;
}

- (IBAction)close:(id)sender{
	ReversiVirtualPlayers* blackCustomDataSource = [blackPlayerComboBox dataSource];
	NSObject<ReversiPlaying>* blackPlayer = [blackCustomDataSource playerObjectAtIndex:[blackPlayerComboBox indexOfSelectedItem]];
	ReversiVirtualPlayers* whiteCustomDataSource = [whitePlayerComboBox dataSource];
	NSObject<ReversiPlaying>* whitePlayer = [whiteCustomDataSource playerObjectAtIndex:[whitePlayerComboBox indexOfSelectedItem]];

	[blackPlayer setUpPlayer];
	[whitePlayer setUpPlayer];

	NSObject<ReversiBoardView>* view = [[ReversiBoardViewString alloc] init];
	[mainView addSubview:[view view]];
	[reversiController initReversi:view:blackPlayer:whitePlayer];
	[initializePanel orderOut:self];
	[[NSApplication sharedApplication]
	 endSheet:initializePanel];
}

- (IBAction)blackSelectItem:(id)sender{
	blackSelectedItemIndex = [blackPlayerComboBox indexOfSelectedItem];
	ReversiVirtualPlayers* blackCustomDataSource = [blackPlayerComboBox dataSource];
	id<ReversiPlaying> player = [blackCustomDataSource playerObjectAtIndex:blackSelectedItemIndex];
	[blackDescriptionField setStringValue:[player description]];
	if ([player haveConfigWindow]){
		[blackConfigButton setEnabled:YES];
	}else {
		[blackConfigButton setEnabled:NO];
	}
}

- (IBAction)blackOpenConfigWindow:(id)sender{
	ReversiVirtualPlayers* blackCustomDataSource = [blackPlayerComboBox dataSource];
	id<ReversiPlaying> player = [blackCustomDataSource playerObjectAtIndex:blackSelectedItemIndex];
	[player openConfigWindow];
}

- (IBAction)whiteSelectItem:(id)sender{	
	whiteSelectedItemIndex = [whitePlayerComboBox indexOfSelectedItem];
	NSLog(@"%d", whiteSelectedItemIndex);
	ReversiVirtualPlayers* whiteCustomDataSource = [whitePlayerComboBox dataSource];
	id<ReversiPlaying> player = [whiteCustomDataSource playerObjectAtIndex:whiteSelectedItemIndex];
	[whiteDescriptionField setStringValue:[player description]];
	if ([player haveConfigWindow]){
		[whiteConfigButton setEnabled:YES];
	}else {
		[whiteConfigButton setEnabled:NO];
	}	
}

- (void)windowDidBecomeKey:(NSNotification *)notification{
	ReversiVirtualPlayers* blackCustomDataSource = [blackPlayerComboBox dataSource];
	[blackCustomDataSource loadPlugIns];
	[blackPlayerComboBox reloadData];
	[blackPlayerComboBox selectItemAtIndex:blackSelectedItemIndex];
	[blackPlayerComboBox performClick:self];
	
	ReversiVirtualPlayers* whiteCustomDataSource = [whitePlayerComboBox dataSource];
	[whiteCustomDataSource loadPlugIns];
	[whitePlayerComboBox reloadData];
	[whitePlayerComboBox selectItemAtIndex:whiteSelectedItemIndex];
	[whitePlayerComboBox performClick:self];	
}

- (IBAction)whiteOpenConfigWindow:(id)sender{
	ReversiVirtualPlayers* whiteCustomDataSource = [whitePlayerComboBox dataSource];
	id<ReversiPlaying> player = [whiteCustomDataSource playerObjectAtIndex:whiteSelectedItemIndex];
	[player openConfigWindow];
}

@end
